<template>
  <el-dialog v-if="visible" :title="info.title" :visible.sync="visible" append-to-body center width="1260px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="120px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="8">
          <el-form-item label="项目名称" prop="pro">
            <ProSelectObject v-model="form.pro" :id="form.pro_id" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item style="height: 37px; overflow: hidden" label="PO单号">
            <span v-if="form.pro.no">{{ "PO"+form.pro.no }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库日期" prop="warehousing_time">
            <el-date-picker
              v-model="form.warehousing_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="附件上传" prop="files">
            <Upload v-model="form.files" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-table class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
            <el-table-column label="序号" align="center" width="56px">
              <template slot-scope="{$index}">
                {{ $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column label="项目名称" width="260px" prop="pro_name" />
            <el-table-column label="PO单号" width="190px" prop="po" />
            <el-table-column label="合同编号" width="190px" prop="lopo" />
            <el-table-column label="品名" width="260px" prop="name" />
            <el-table-column label="出库数" align="center" width="180px">
              <template slot-scope="{row}">
                <el-input-number v-model="row.num" size="mini" :max="row.total - row.ex" :min="0" :step="1" />
              </template>
            </el-table-column>
            <el-table-column label="总数" align="right" width="80px" prop="total" />
            <el-table-column label="已出库数" align="right" width="80px" prop="ex" />
            <el-table-column label="规格型号" width="120px" prop="sku" />
            <el-table-column label="参数" width="260px" prop="parameter" />
            <el-table-column label="品牌" width="140px" prop="brand" />
            <el-table-column label="单位" width="140px" prop="unit" />
          </el-table>
        </el-col>
        <!-- <el-col class="mt10" :span="24">
          <el-form-item label="抄送" prop="ccs">
            <VerifyCC v-model="form.ccs" :ccs="ccs" />
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleBeforeTo()">提 交</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import ProSelectObject from '@/myComponents/ProSelectObject'
// import VerifyCC from '@/myComponents/VerifyCC'
import { warehousingInfos } from '@/api/purchase/purchase'
import Upload from '@/myComponents/Upload'
import svf from '../../util/svf'

export default {
  components: { ProSelectObject, Upload },
  extends: svf,
  data() {
    const vnull = (rule, value, callback) => {
      if (JSON.stringify(value) === '{}') {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    }
    return {
      approve_key: 'PROCESS_WAREHOUSING',
      list: [],
      rules: {
        pro: [{ required: true, trigger: 'blur', message: '项目名称不可为空', validator: vnull }],
        warehousing_time: [{ required: true, trigger: 'blur', message: '入库日期不可为空' }],
        files: [{ required: true, trigger: 'blur', message: '附件不可为空' }]
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        if(this.info.type && this.info.type === 'again'){
          this.initApply(this.info.edit)
        }else{
          this.form = this.info.edit
        }
        this.$set(this.form, 'pro', {})
        // this.queryCcs()
      }
    },
    'form.pro': {
      handler(n, o) {
        if(this.form.pro.key) {
          this.getList(this.form.pro.key)
        }
      },
      immediate: false
    }
  },
  mounted() {
  },
  methods: {
    getList(e) {
      warehousingInfos({pro_id: e}).then(response => {
        this.list = response.list
      }).catch(err => {
        this.$message.error(err.msg ? err.msg : '获取数据失败')
      })
    },
    handleBeforeTo() {
      this.validate().then(() => {
        const e = {}
        e.files = this.form.files
        e.approve_key = this.approve_key
        e.des = '项目名称：' + this.form.pro.value + '    入库日期' + this.form.warehousing_time + '    备注：' + this.form.remark
        e.ccs = this.form.ccs
        e.forms = JSON.stringify(this.createForms([
          ['PO单号', 'PO' + this.form.pro.no, '', ''],
          ['项目名称', this.form.pro.value, this.form.pro.key, 'pro_id'],
          ['入库日期', this.form.warehousing_time, this.form.warehousing_time, 'warehousing_time'],
          ['备注', this.form.remark, this.form.remark, 'remark']
        ]))
        let tables = {}
        tables.list = this.list
        tables.columns = [{'label':'项目名称', 'prop':'pro_name'}, {'label':'PO单号', 'prop':'po'}, {'label':'合同编号', 'prop':'lopo'}, 
                          {'label':'品名', 'prop':'name'}, {'label':'单价', 'prop':'price'}, {'label':'出库数', 'prop':'num'}, {'label':'总数', 'prop':'total'}, {'label':'已出库数', 'prop':'ex'},
                          {'label':'规格型号', 'prop':'sku'}, {'label':'参数', 'prop':'parameter'}, {'label':'品牌', 'prop':'brand'}, {'label':'单位', 'prop':'unit'}]
        e.tables = JSON.stringify(tables)
        this.handleCreate(e)
      })
    }
  }
}
</script>

<style>
</style>
